﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RenderEngineInterfaces;
using System.ComponentModel.Composition;
using System.Threading.Tasks;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using DeepZoomPivotConstructor.VisualLib;
using System.Drawing;
using System.IO;

namespace t_TestingLibrary
{
    public class Utilities
    {
        /// <summary>
        /// Holds the crap for MEF.
        /// </summary>
        private static CompositionContainer _container = null;

        /// <summary>
        /// Keep track of everyone we are going to compose.
        /// </summary>
        private static CompositionBatch _batch = null;

        private static AggregateCatalog _catalog;

        /// <summary>
        /// Call this at the start of the class in order to get MEF setup.
        /// </summary>
        /// <param name="context"></param>
        public static void MyClassInit()
        {
            _catalog = new AggregateCatalog();

            _container = new CompositionContainer(_catalog);
            _batch = new CompositionBatch();
        }

        /// <summary>
        /// We are done - make sure nothing gets accidentally used
        /// </summary>
        public static void MyClassDone()
        {
            _batch = null;
            _catalog = null;
            _container.Dispose();
            _container = null;
        }

        public static void AddJPEGRender()
        {
            RenderJpegFromString j = new RenderJpegFromString();
            _batch.AddPart(j);
        }

        public static void AddAssemblyForType(Type myType)
        {
            _catalog.Catalogs.Add(new AssemblyCatalog(myType.Assembly));
        }

        /// <summary>
        /// Do the composition
        /// </summary>
        /// <param name="re"></param>
        public static void Compose<T>(T re)
        {
            _batch.AddPart(re);
            _container.Compose(_batch);
        }

        public static void AddJPEGFileInfoRender()
        {
            _batch.AddPart(new RenderJpegFromFileInfo());
        }

        public static void AddPSFileInfoRender()
        {
            _batch.AddPart(new RenderPSFromFileInfo());
        }

        public static void AddPSFileInfoRenderHigh()
        {
            _batch.AddPart(new RenderPSFromFileInfoHigh());
        }

        public static void AddPart<T>(T o)
        {
            _batch.AddPart(o);
        }
    }

    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(string))]
    class RenderJpegFromString : IRenderItem
    {
        #region IRenderItem Members

        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
            {
                Visual result = new VisualBitmapImage(1, 2, 100, Color.Red);
                return result;
            });
        }

        public RenderPluginPriority RenderPriority(object what)
        {
            string f = what as string;
            if (f.EndsWith(".jpeg") || f.EndsWith(".jpg"))
            {
                return RenderPluginPriority.RPPGeneric;
            }
            return RenderPluginPriority.RPPCantDoIt;
        }

        #endregion
    }

    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(FileInfo))]
    public class RenderJpegFromFileInfo : IRenderItem
    {
        #region IRenderItem Members

        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
            {
                Visual result = new VisualBitmapImage(1, 2, 100, Color.Red);
                return result;
            });
        }

        public RenderPluginPriority RenderPriority(object what)
        {
            FileInfo f = what as FileInfo;
            if (f.Extension == ".jpeg" || f.Extension == ".jpg")
            {
                return RenderPluginPriority.RPPGeneric;
            }
            return RenderPluginPriority.RPPCantDoIt;
        }

        #endregion
    }

    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(FileInfo))]
    class RenderPSFromFileInfo : IRenderItem
    {
        #region IRenderItem Members

        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
            {
                Visual result = new VisualBitmapImage(2, 3, 100, Color.Red);
                return result;
            });
        }

        public RenderPluginPriority RenderPriority(object what)
        {
            FileInfo f = what as FileInfo;
            if (f.Extension == ".ps" || f.Extension == ".eps")
            {
                return RenderPluginPriority.RPPGeneric;
            }
            return RenderPluginPriority.RPPCantDoIt;
        }

        #endregion
    }

    [Export(typeof(IRenderItem))]
    [RenderInfo(typeof(FileInfo))]
    class RenderPSFromFileInfoHigh : IRenderItem
    {
        #region IRenderItem Members

        public Task<Visual> Render(object what)
        {
            return Task<Visual>.Factory.StartNew(() =>
            {
                Visual result = new VisualBitmapImage(3, 4, 100, Color.Red);
                return result;
            });
        }

        public RenderPluginPriority RenderPriority(object what)
        {
            FileInfo f = what as FileInfo;
            if (f.Extension == ".ps" || f.Extension == ".eps")
            {
                return RenderPluginPriority.RPPSpecific;
            }
            return RenderPluginPriority.RPPCantDoIt;
        }

        #endregion
    }
}
